package com.atguigu.gulimall.product.web;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.feign.CartFeignService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.redisson.client.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    StringRedisTemplate redisTemplate ;
    @Autowired
    CartFeignService cartFeignService ;
    @Autowired
    RedissonClient redisson;
    @GetMapping({"/","index.html"})
    public String indexPage(Model model){
        List<CategoryEntity> categoryEntityList = categoryService.getLevel1Category();
        model.addAttribute("categorys",categoryEntityList);
        R r = cartFeignService.cartSize();
        model.addAttribute("cartSize",r.get("cartSize"));
        return "index";
    }
    @ResponseBody
@GetMapping("/index/json/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson(){
        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson ;
    }
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        RLock lock = redisson.getLock("my-lock");
//        lock.lock();
        lock.lock(10, TimeUnit.SECONDS);
        try {
            System.out.println("加锁成功，执行任务。。。"+Thread.currentThread().getId());
            Thread.sleep(30000);
        }catch (Exception e){}finally {
            System.out.println("释放锁。。。"+Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello" ;
    }
    @ResponseBody
    @GetMapping("/write")
    public String writeValue(){
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = lock.writeLock();
        try {
            rLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writeValue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s ;
    }
    @ResponseBody
    @GetMapping("/read")
    public String readValue(){
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = lock.readLock();
        rLock.lock();
        try {
            s= redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s ;
    }

//    车库停车 3个车位
    @GetMapping("/park")
    @ResponseBody
    public String park(){
        RSemaphore park = redisson.getSemaphore("park");
        boolean b = park.tryAcquire();//获取一个值
        if(b){
            //执行业务
        }else{
            return "error";
        }
        return "ok=>"+b;
    }
    @GetMapping("/go")
    @ResponseBody
    public String go(){
        RSemaphore park = redisson.getSemaphore("park");
            park.release();
        return "ok";
    }
    /*
放假锁门
    全部走完才可以锁门
     */
    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "放假了 。。。";
    }
    @GetMapping("/gogogo/{id}")
    public String gogogo(@PathVariable("") Long id){
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.countDown();
        return   id+"班的人都走了。。。";
    }
}
